package com.github.permissiondog.cancanpandroid.ui.view;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.github.permissiondog.cancanpandroid.R;
import com.github.permissiondog.cancanpandroid.databinding.ActivityApplicationApproveBinding;
import com.github.permissiondog.cancanpandroid.databinding.ActivityApplicationProgressBinding;
import com.github.permissiondog.cancanpandroid.model.ApplicationStatus;
import com.github.permissiondog.cancanpandroid.repository.impl.ApplicationRepositoryImpl;
import com.github.permissiondog.cancanpandroid.util.Constants;
import com.github.permissiondog.cancanpandroid.util.NetUtil;

public class ApplicationProgressActivity extends AppCompatActivity {
    private int applicationId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        applicationId = getIntent().getIntExtra("applicationId", -1);
        Log.d("ApplicationApprove", "applicationId: " + applicationId);

        ActivityApplicationProgressBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_application_progress);
        ApplicationRepositoryImpl.getInstance().getApplicationInfo(applicationId, appInfo -> {
            binding.tvCourseName.setText(appInfo.getTemplate().getTableName());
            appInfo.getQuestions().forEach(question -> {
                switch (question.getQuestion().getTitle()) {
                    case "申请原因" -> {
                        if (question.getAnswer() != null)
                            binding.tvReason.setText(question.getAnswer().getAnswer());
                    }
                    case "上传证明" -> {
                        if (question.getAnswer() != null)
                            NetUtil.PICASSO
                                    .load(Constants.BASE_URL + "resource/" + question.getAnswer().getAnswer())
                                    .into(binding.ivImageUploaded);
                    }
                }
            });
            if (appInfo.isClosed() && !appInfo.getApplication().isConfirmed()) {
                binding.btnConfirmApplication.setVisibility(View.VISIBLE);
            } else {
                binding.btnConfirmApplication.setVisibility(View.GONE);
            }

            switch (appInfo.getStatus()) {
                case PENDING -> {
                    if (appInfo.getProgress().size() == 0) {
                        binding.tvStatus.setText("课程主讲教师审批中");
                    } else {
                        binding.tvStatus.setText("课程主管教师审批中");
                    }
                }
                case APPROVED -> {
                    if (appInfo.getApplication().isConfirmed()) {
                        binding.tvStatus.setText("已确认审批通过");
                    } else {
                        binding.tvStatus.setText("待确认审批通过");
                    }
                }
                case REJECTED -> {
                    if (appInfo.getApplication().isConfirmed()) {
                        binding.tvStatus.setText("已确认审批拒绝");
                    } else {
                        binding.tvStatus.setText("待确认审批拒绝");
                    }
                }
            }

        });
        binding.btnConfirmApplication.setOnClickListener(l ->
                ApplicationRepositoryImpl.getInstance().confirmApplication(applicationId, ok -> {
            var intent = new Intent();
            intent.setAction(Constants.ACTION_REFRESH_APPLICATIONS);
            sendBroadcast(intent);
            finish();
        }));
    }
}